package
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.display.BlendMode;
	import flash.filters.BlurFilter;
	import flash.geom.Point;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.display.MovieClip;
	
	[SWF(width="550",height="400",backgroundColor="0x000000")]
	public class WriteFire extends Sprite
	{
		//创建MovieClip类实例，并赋值给变量btn
		private var btn:MovieClip = new MovieClip();  
		//=================完成写字效果
		private var sp:Sprite =new Sprite();
		//====== 下面是火焰效果
		private var map:BitmapData;
		
		private var mc:Sprite =new Sprite();
		private var mapmc:Sprite =new Sprite();
		
		private var mapz:BitmapData;
		private var mapb:BitmapData;
		
		public function WriteFire()
		{
			btn.graphics.beginFill(0xFFFF00,100)  
			btn.graphics.drawRect(0,0,30,20)

			stage.addEventListener(MouseEvent.MOUSE_DOWN ,down);
			
			btn.addEventListener(MouseEvent.MOUSE_DOWN ,shanchu);
			
			mapz=new BitmapData(550,400,false,0);
			mapb=new BitmapData(550,400,false,0);
			
			writeMap();
		}
		
		private function writeMap():void
		{
			map=new BitmapData(550,400,false,0);
			
			//15 ，40代表xy方向的值， 3是层数，true 表哈斯缝合， true 表示碎片的噪声， 
			//10 是随机总数， 1代表红色， true 代表红绿蓝相同的值
			
			map.perlinNoise(15,40,3,10,true,true,1,true);
			mc.addChild(mapmc);
			
			for (var i:int=0; i<3; i++) 
			{
				var mapbit:Bitmap=new Bitmap(map);
				mapmc.addChild(mapbit);
				mapbit.y=i*400;
			}
			
			addChild(new Bitmap(mapz));
			
			var bit:Bitmap=new Bitmap(mapb);
			addChild(bit);
			bit.blendMode=BlendMode.OVERLAY;// 混合模式
			
			addEventListener(Event.ENTER_FRAME ,frame);
			
			addChild(btn)
		}
		
		private function down(e):void
		{
			if (e.target!=btn) 
			{
				// 这句判断为了防止鼠标放在按钮上画线：如果当前目标不等于按钮时画线
				sp.graphics.lineStyle(5,0xffff00);
				sp.graphics.moveTo(mouseX,mouseY);
				stage.addEventListener(MouseEvent.MOUSE_MOVE,movef);
				stage.addEventListener(MouseEvent.MOUSE_UP,upf);
				stage.removeEventListener(MouseEvent.MOUSE_DOWN ,down);
			}
		}
		
		private function movef(e):void
		{
			sp.graphics.lineTo(mouseX,mouseY);
		}
		
		private function upf(e):void
		{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,movef);
			stage.removeEventListener(MouseEvent.MOUSE_UP,upf);
			stage.addEventListener(MouseEvent.MOUSE_DOWN ,down);
		}
		
		private function shanchu(e):void
		{
			sp.graphics.clear();
		}
		
		private function frame(e):void
		{
			mapz.draw(sp);
			mapb.draw(mc);
			mapz.applyFilter (mapz,mapz.rect ,new Point(),new BlurFilter(2,2,1))
			mapz.colorTransform (mapz.rect ,new ColorTransform(1,1,0.5,1,-5,-20,-20))
			mapz.draw (mapz.clone (),new Matrix(1,0,0,1,0,-2))
			mapmc.y>-800?(mapmc.y-=5,mapmc.x+=Math.random ()*10-5):(mapmc.y=0,mapmc.x=0)
		}
	}
}